<?php

/**
 * B2B贸易接口扩展
 * @author wyb
 */
namespace DataAPI\Extend;
header("Content-type:text/html;charset=utf-8");

class ExtendApi {

    private $_app_id = ''; //商家ID
    private $_app_key = ''; //商家加密 KEY
    private $_method = ''; //请求接口
    private $_allow_method = 'post'; //请求方法
    private $_url = 'http://dc.shenghuigo.com/api.php';
    private $_error = ''; //错误信息
    private $_data = array();
    public static $_self = null;

    public static function init($app_id = '', $app_key = '') {
        return self::$_self == null ? new self($app_id , $app_key) : self::$_self;
    }

    public function __construct($app_id , $app_key )
    {
        $this->_app_key = $app_key;
        $this->_app_id = $app_id;
    }
    // ===============================================     商品操作接口     =================================================
    /**
     * 商品列表查询
     * @param int $page 分页数量
     * @param int $row  每页显示数量
     * @param int $record  是否调取所有数据 1所有数据   0最新数据
     */
    public function goodsLists($page = 1, $row = 20, $record = 1) {
        $this->_method = 'Goods.lists';
        $this->_allow_method = 'get';
        $this->_data = array(
            'page' => $page,
            'row' => $row,
            'record' => $record
        );
        return $this->_postHttp();
    }

    /**
     * 商品库存查询
     * @param string $earn13 商品EN13码 多个请用","隔开
     */
    public function goodsRcount($earn13) {
        $this->_method = 'Goods.get_product_rcount';
        $this->_allow_method = 'get';
        $this->_data = array(
            'earn13' => $earn13
        );
        return $this->_postHttp();
    }

    /**
     * 商品价格查询
     * @param string $earn13 商品EN13码 多个请用","隔开
     */
    public function goodsPrice($earn13) {
        $this->_method = 'Goods.get_product_price';
        $this->_allow_method = 'get';
        $this->_data = array(
            'earn13' => $earn13
        );
        return $this->_postHttp();
    }

    /**
     * 商品库存更新
     * @param string $earn13 商品EN13码
     * @param string $stock_num 库存数量
     */
    public function goodsSaveStock($earn13, $stock_num) {
        $this->_method = 'Goods.save_stock';
        $this->_allow_method = 'post';
        $this->_data = array(
            'earn13' => $earn13,
            'stock_num' => $stock_num
        );
        return $this->_postHttp();
    }

    /**
     * 商品添加
     * @param array $data 商品数据
     */
    public function goodsAdd($data = array()) {
        $this->_method = 'Goods.goods_add';
        $this->_allow_method = 'post';
        $this->_data = $data;
        return $this->_postHttp();
    }

    /**
     * 商家认证
     */
    public function customerChecked() {

        $this->_method = 'Check.checked';
        $this->_allow_method = 'post';
        return $this->_postHttp();
    }

    /**
     * 错误信息
     */
    public function getError() {
        return $this->_error;
    }

    //发送信息到服务端
    private function _postHttp() {
        $this->_data['app_id'] = $this->_app_id;
        $this->_data['method'] = $this->_method;
        $trade_data = array();
        ksort($this->_data);
        foreach ($this->_data as $key => $value) {
            if (is_array($value)) {
                $trade_data[$key] = "{$key}=" . json_encode($value);
            } else if ($value != '') {
                $trade_data[] = "{$key}={$value}";
            }
        }
        $this->_data['sgin'] = strtoupper(md5(implode('&', $trade_data) . '&key=' . $this->_app_key));
        $chs = curl_init();
        curl_setopt($chs, CURLOPT_URL, $this->_url . ( $this->_allow_method == 'post' ? '' : '?' . http_build_query($this->_data) ));
        curl_setopt($chs, CURLOPT_RETURNTRANSFER, 1);
        if ($this->_allow_method == 'post') {
            curl_setopt($chs, CURLOPT_POST, 1);
            curl_setopt($chs, CURLOPT_POSTFIELDS, $this->_data);
        }
        $output = curl_exec($chs);
        $httpCode = curl_getinfo($chs, CURLINFO_HTTP_CODE);
        curl_close($chs);

        //判断服务器通信错误信息
        $result = json_decode($output, true);
        if ( $httpCode >= 500 || $httpCode == 404 ) {
            $this->_error = '服务器错误';
            return $this->_error;
        }
        $result = json_decode($output, true);
        //判断数据是否完整
        if (!$result) {
            $this->_error = '数据解析错误';
            return $this->_error;
        }
        //判断服务器业务逻辑层返回的信息
        if ($result['code'] != 200) {
            $this->_error = $result['msg'];
            return $this->_error;
        }
        //判断返回数据还是真假
        if (!isset($result['data'])) {
            return true;
        } else {
            return $result['msg'];
        }
    }

}
